package me.siphone.view.grouptree;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import me.siphone.Constants;
import org.linphone.R;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {
    private List<Device> devices;

    public interface DeviceCheckedChangedListener {
        void onDeviceCheckedChanged(Device device);
    }

    private DeviceCheckedChangedListener listener;

    public DeviceAdapter(List<Device> devices, DeviceCheckedChangedListener listener) {
        this.devices = devices;
        this.listener = listener;
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View view =
                    LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_no_device, parent, false);
            return new DeviceViewHolder(view);
        }
        View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        if (devices == null || position >= devices.size()) {
            return;
        }

        Device device = devices.get(position);
        holder.deviceName.setText(device.name);
        holder.deviceCheckBox.setChecked(device.isChecked);

        if (device.isActive) {
            holder.deviceCheckBox.setEnabled(true);
            holder.deviceCheckBox.setOnCheckedChangeListener(
                    (buttonView, isChecked) -> {
                        device.isChecked = isChecked;
                        Log.d(Constants.TAG, "设备选择：" + isChecked);
                        if (listener != null) listener.onDeviceCheckedChanged(device);
                    });
            holder.deviceName.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.deviceCheckBox.setChecked(!holder.deviceCheckBox.isChecked());
                        }
                    });
            holder.deviceCheckBox.setActivated(true);
            holder.deviceName.setActivated(true);
        } else {
            holder.deviceCheckBox.setEnabled(false);
            holder.deviceCheckBox.setOnCheckedChangeListener(null);
            holder.deviceCheckBox.setActivated(false);
            holder.deviceName.setActivated(false);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return (devices == null || devices.isEmpty()) ? -1 : position;
    }

    @Override
    public int getItemCount() {
        return devices == null || devices.isEmpty() ? 1 : devices.size();
    }

    static class DeviceViewHolder extends RecyclerView.ViewHolder {
        CheckBox deviceCheckBox;
        TextView deviceName;

        DeviceViewHolder(View itemView) {
            super(itemView);
            deviceCheckBox = itemView.findViewById(R.id.deviceCheckBox);
            deviceName = itemView.findViewById(R.id.deviceName);
        }
    }
}
